package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;



import DomainLayer.Journal;
import DomainLayer.TemperatureSchemeMeasurement;

public class DBTemperatureScheme implements IFDBTemperatureScheme {

	private Connection con;

	public DBTemperatureScheme() {
		con = DbConnection1.getInstance().getDBcon();
	}

	@Override
	public int createScheme(TemperatureSchemeMeasurement temperatureScheme) {

		int rc = -1;
		
		String query2 = "Insert into MeasurementsTempScheme values ('"
				+ temperatureScheme.getNameScheme() + "',"
				+ temperatureScheme.getPuls() + ","
				+ temperatureScheme.getRespiration() + ","
				+ temperatureScheme.getBlood_pressure_systolic() + ","
				+ temperatureScheme.getBlood_pressure_diastolic() + ",'"
				+ temperatureScheme.getDatetime() + "',"
				+ temperatureScheme.getTemperature() + ")";

		
		System.out.println("insert : " + query2);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query2);
			
			stmt.close();
		} catch (SQLException ex) {
			System.out.println("problem while adding temp scheme");

		}
		return rc;
	}//44

	@Override
	public int updateScheme(TemperatureSchemeMeasurement temperatureScheme) {
		int rc = 0;

		String query = "Update MeasurementsTempScheme set puls="
				+ temperatureScheme.getPuls() + " ,respiration="
				+ temperatureScheme.getRespiration()
				+ ",blood_pressure_systotic="
				+ temperatureScheme.getBlood_pressure_systolic()
				+ ",blood_pressure_dialstotic="
				+ temperatureScheme.getBlood_pressure_diastolic()
				+ ",datetime='" + temperatureScheme.getDatetime() + "' where tempSchemeName='"+temperatureScheme.getNameScheme()+"'";

		System.out.println("Update: " + query);

		try { // update temperatureScheme
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);

			stmt.close();
		} catch (Exception ex) {
			System.out.println("Update exception in temp scheme db: " + ex);
		}
		return rc;
	}

	public int deleteScheme(String schemeName) {
		int rc = -1;

		String query = "DELETE FROM MeasurementsTempScheme WHERE tempSchemeName ='"
				+ schemeName + "'";
		System.out.println(query);
		try { // delete from OrderLine
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		}// end try
		catch (Exception ex) {
			System.out.println("Delete exception in sales order db: " + ex);
		}
		return (rc);
	}

	@Override
	public TemperatureSchemeMeasurement getScheme(String schemeName) {
		String wClause = " tempSchemeName = '" + schemeName + "'";
		return singleWhere(wClause);
	}

	@Override
	public ArrayList<TemperatureSchemeMeasurement> getAllTemperatureScheme() {
		return miscWhere("");
	}

	private ArrayList<TemperatureSchemeMeasurement> miscWhere(String wClause) {
		ResultSet results;
		ArrayList<TemperatureSchemeMeasurement> list = new ArrayList<TemperatureSchemeMeasurement>();

		String query = buildQuery(wClause);

		try { // read the employee from the database
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			while (results.next()) {
				TemperatureSchemeMeasurement scheme = new TemperatureSchemeMeasurement();
				scheme = buildTemperatureScheme(results);
				list.add(scheme);
			}// end while
			stmt.close();

		}// slut try
		catch (Exception e) {
			System.out.println("Query exception - select: " + e);
			e.printStackTrace();
		}
		return list;

	}

	private TemperatureSchemeMeasurement singleWhere(String wClause) {
		ResultSet results;
		TemperatureSchemeMeasurement scheme = new TemperatureSchemeMeasurement();

		String query = buildQuery(wClause);
		System.out.println(query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			if (results.next()) {
				scheme = buildTemperatureScheme(results);
				stmt.close();

			} else {
				scheme = null;
			}
		} catch (Exception e) {
			System.out.println("Query exception: " + e);
		}
		return scheme;

	}

	private String buildQuery(String wClause) {
		String query = " Select * from MeasurementsTempScheme ";
		if (wClause.length() > 0)
			query = query + " WHERE" + wClause;

		return query;
	}

	private TemperatureSchemeMeasurement buildTemperatureScheme(ResultSet results) {
		TemperatureSchemeMeasurement scheme = new TemperatureSchemeMeasurement();
		try {
			scheme.setNameScheme(results.getString(1));
			scheme.setPuls(results.getInt(2));
			scheme.setRespiration(results.getInt(3));
			scheme.setBlood_pressure_systolic(results.getInt(4));
			scheme.setBlood_pressure_diastolic(results.getInt(5));
			scheme.setDatetime(results.getString(6));
			scheme.setTemperature(results.getFloat(7));

		} catch (Exception e) {
			System.out.println("error in building the temperature scheme");
		}

		return scheme;

	}

	public ArrayList<TemperatureSchemeMeasurement> getAllTemperatureSchemeMeasurements(String name)
	{
		 String wClause = " tempSchemeName = '" + name + "'";
		 return miscWhere(wClause);
	}
	
	
	
	public ArrayList<TemperatureSchemeMeasurement> getTemperatureSchemeForJournal(Journal journal){
		ResultSet results;
		ArrayList<TemperatureSchemeMeasurement> list = new ArrayList<TemperatureSchemeMeasurement>(); 
		String query="SELECT * FROM MeasurementsTempScheme where tempSchemeName=(select top 1 tempSchemeName From TempScheme WHERE JournalName='" +journal.getName()+"'"
				 + "and contactCauses='" +journal.getContact_causes()+ "')";
		 System.out.println("Query getTemperatureSchemeForJournal:" + query);
			try { // read the employee from the database
				Statement stmt = con.createStatement();
				stmt.setQueryTimeout(5);
				results = stmt.executeQuery(query);

				while (results.next()) {
					TemperatureSchemeMeasurement scheme = new TemperatureSchemeMeasurement();
					scheme = buildTemperatureScheme(results);
					list.add(scheme);
				}// end while
				stmt.close();

			}// slut try
			catch (Exception e) {
				System.out.println("Query exception - select: " + e);
				e.printStackTrace();
			}
			return list;
		 
		 
		
		
	}
}
